package com.gxuwz.business.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.gxuwz.business.entity.StandFee;
import com.gxuwz.util.Pager;

@Repository("standFeeDAO")
public class StandFeeDAO extends BaseDaoImpl<StandFeeDAO>{

	/**
	 * 分页查询(条件查询)
	 */
	public Pager<StandFee> getByPager(StandFee standFee, Integer page, Integer limit){
		String hql = "from StandFee where 1=1 ";
		//模糊查询
		if(null != standFee.getStandName() && !"".equals(standFee.getStandName())){
			String key = standFee.getStandName();
			hql += " and (standId like '%"+key+"%' or standName like '%"+key+"%' or count like '%"+key+"%' "
					+ "or totalFee like '%"+key+"%' or userName like '%"+key+"%')";
		}
		return super.find(hql, null, null, (page-1)*limit, limit);
	}
	
	/**
	 * 添加费用标准信息
	 */
	public boolean add(StandFee standFee){
		try{
			getHibernateTemplate().save(standFee);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 检测标准格式是否已存在
	 */
	public boolean checkStandName(StandFee standFee){
		return checkExist(StandFee.class, "standName", standFee.getStandName());
	}
	
	/**
	 * 伪删除
	 */
	public boolean delete(StandFee standFee){
		boolean status = false;
		String hql = "update StandFee set status = 1 where standId = '"+standFee.getStandId()+"'";
		try{
			getHibernateTemplate().bulkUpdate(hql);
			status = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return status;   
	}
	
	/**
	 * 根据治疗费用标准编号查询
	 */
	public StandFee findById(StandFee standFee){
		String hql = "from StandFee where standId = '"+standFee.getStandId()+"'";
		try {
			return (StandFee) getHibernateTemplate().find(hql, null).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 只更新姓名、性别、年龄、年级、学院、班别、医保类型、挂号类型
	 */
	public boolean update(StandFee s){
		boolean isSucceed = false;
		String hql = "update StandFee set standName=?, count=? where standId=?";
		try {
			int count = super.getHibernateTemplate().bulkUpdate(hql, s.getStandName(),s.getCount(),s.getStandId());
//			System.out.println("更改了"+count+"条数据");
			isSucceed = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isSucceed;
	}
	
	/**
	 * 查询挂号编号对应全部费用
	 */
	public List<Double> getFeeByRegId(String regId){
		String hql = "select totalFee from StandFee where regId = ?";
		try {
			@SuppressWarnings("unchecked")
			List<Double> feeList = (List<Double>) getHibernateTemplate().find(hql, regId);
			return feeList;
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return null;
	}
	
	
}
